package com.song.controller;


import ch.qos.logback.core.testUtil.RandomUtil;
import com.aliyuncs.utils.StringUtils;

import com.song.service.SmsService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Controller
public class SmsController {

    @Resource
    private SmsService smsService;

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @RequestMapping("/send")
    @ResponseBody
    public boolean send(@RequestParam("phone") String phone){
        String code="";
        //随机6位数
        Random random = new Random();
        int randomNumber = random.nextInt(900000) + 100000;
        code=String.valueOf(randomNumber);

        Map<String,Object> map=new HashMap<>();
        map.put("code",code);
        boolean b=smsService.send(phone,map);
        if(b){
            //存入redis，有效期1分钟
            this.redisTemplate.opsForValue().set(phone+"_"+code, randomNumber+"",1,TimeUnit.MINUTES);
            return true;
        }else{
            return false;
        }
    }
}
